package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreCompensation;
import com.edu.hre.db.pojo.HreEmpBasicsalary;
import com.edu.hre.db.pojo.HreEmpCompensation;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpCompensationForm;
import com.edu.hre.struts.form.pim.EmpCompensationListForm;

public class EmpCompensationDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empCompensationDAOService";
	private static BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpCompensationForm> getEmpCompensationList(int limit, int offset, int empNumber){

		ArrayList<EmpCompensationForm> listResult = new ArrayList<EmpCompensationForm>();

		List<HreEmpCompensation> listHre = baseService.getPojos("empNumber", Compare.EQUAL, empNumber, new String[]{"year", "month"}, new Boolean[]{true, true}, limit, offset, HreEmpCompensation.class);
		if (listHre == null) {
			return listResult;
		}
		
		int totalKpi = 0;
		double amount = 0;
		for (HreEmpCompensation empCom: listHre) {
			EmpCompensationForm comForm = new EmpCompensationForm();
			comForm.setEmpNumber(empNumber);
			comForm.setKpi(empCom.getKpi() + "");
			if (empCom.getMonth() > 0) {
				comForm.setMonth(StringConstants.ALL_MONTHS[empCom.getMonth() - 1]);
			}
			comForm.setYear(empCom.getYear() + "");
			
			HreCompensation hreCompQuota = baseService.getFirstPojoAnd(new String[]{"year", "month"}, new int[]{Compare.EQUAL, Compare.EQUAL}, new Object[]{empCom.getYear(), empCom.getMonth()}, HreCompensation.class);
			if (hreCompQuota == null) {
				amount = 0;
			} else {
				amount = hreCompQuota.getAmount();
			}
			
			totalKpi = 0;
			List<HreEmpCompensation> hreListKpi = baseService.getPojosAnd(new String[]{"year", "month"}, new int[]{Compare.EQUAL, Compare.EQUAL}, new Object[]{empCom.getYear(), empCom.getMonth()}, null, null, -1, 0, HreEmpCompensation.class);
			if (hreListKpi != null) {
				for (HreEmpCompensation ec: hreListKpi) {
					totalKpi += ec.getKpi();
				}
			}
			
			if (totalKpi != 0) {
				comForm.setAmount(String.format("%,15.2f", amount * empCom.getKpi() / totalKpi) + " VND");
			} else {
				comForm.setAmount("0");
			}
			
			comForm.setTotalAmount(String.format("%,15.2f", amount) + " VND");
			comForm.setTotalKpi(totalKpi + "");

			listResult.add(comForm);
		}

		return listResult;
	}

	public int countEmpCompensations(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpCompensation.class);
		return count < 0 ? 0 : count;
	}

	public void deleteCompensations(EmpCompensationListForm form){

		ArrayList<EmpCompensationForm> info = (ArrayList<EmpCompensationForm>) form.getAllCompensations();

		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpCompensationForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, work.getId(), null, null, 0, HreEmpCompensation.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}

